var stars = liwe.module ( "stars" );

stars._cache = {
			id_obj: '',
			module: '',
			site_id: '',
			results : [],
			div_id : ''
			};
stars.param_form = {
			stars_class_on  : 'star-form-on',
			stars_class_off : 'star-form-off',
			tmpl_row 	: 'STARS-row',
			tmpl_cnt	: 'STARS-rows'
			};
stars.param_set = {
			stars_class_on  : 'star-set-on',
			stars_class_off : 'star-set-off',
			tmpl_row 	: 'STARS-set-row',
			tmpl_cnt	: 'STARS-set'
			};

stars._dest_div = 'block_main';


stars.set_dest_div = function ( dest_div )
{
	stars._dest_div = dest_div;
};

stars.init = function()
{
	console.debug ( "stars.init called ");
	stars.load_templates ( function () { console.debug (  'stars templates loaded %o', stars.templates ); } );
};

stars._create_stars = function ( id_obj, module, rank, param )
{
	if ( ! id_obj || ! module ) return;
	if ( ! rank ) rank = 0;
	if ( ! param ) param = stars.param_form;

	var c = 1;
	var vals, star_class, rows = new String.buffer ();
	while ( c <= 5 )
	{
		star_class = ( c <= rank ) ? param.stars_class_on  : param.stars_class_off ;
		vals = {
			'val': c,
			'id_obj': id_obj, 
			'star_class' : star_class, 
			'module' : module,
			'rank': rank
			};
		rows.add ( String.formatDict ( stars.templates [ param.tmpl_row ], vals ) );
		c += 1;
	}
	var out = String.formatDict ( stars.templates [ param.tmpl_cnt ], { id_obj : id_obj, stars_rows : rows } );
	return out;
};

stars.box = function ( id_obj, module, dest_div, id_site, buffer )
{
	if ( ! id_site ) id_site = site._id_site;

	var vals = {
			'id_obj' : id_obj,
			'module' : module,
			'id_site': id_site,
			'action' : 'stars.ajax.obj_rank'
			};
	stars.ajax ( vals, function ( data )
				{
					var dct = {
							'id_obj'     : id_obj,
							'module'     : module,
							'id_site'    : id_site,
							'div_id'     : 'STARS-' + id_obj + '@' + module, 
							'stars_form' : stars._create_stars ( id_obj, module, data [ 'user_rank' ] ),
							'stars_set'  : stars._create_stars ( id_obj, module, data [ 'obj_rank' ], stars.param_set )
						  };
					var output = String.formatDict ( stars.templates [ 'STARS-cnt' ], dct );
					console.debug ( output );
					if ( buffer ) return output; 
					$( dest_div, output );
				} );
};

stars._refresh = function ( id_obj, module, rank )
{
	var dest_div = 'STARS-' + id_obj + '@' + module + '-form'; 
	var out = stars._create_stars ( id_obj, module, rank );
	$( dest_div , out );	
};

stars.set_star = function ( id_obj, module, val , id_site )
{
	if ( ! id_site ) id_site = site._id_site;
	var vals = {
			'id_obj' : id_obj,
			'module' : module,
			'id_site': id_site,
			'rank'   : val,
			'action' : 'stars.ajax.obj_set_rank'
			};
	stars.ajax ( vals, function ( data ) 
				{
					console.debug ( data );
					stars._refresh ( id_obj, module, val ); 		
				} );
};

stars._show_div = function ( target_div, force )
{
        var vis = ''
        if ( force )
        {
                vis = force;
        } else {
                vis = ( $( target_div ).style.display == "none" || ! $( target_div ).style.display ) ? "block" : "none";
        }
        var el_butt = target_div + '-div-butt' ;
        //console.debug ( "--_ show_div:  ( %o ) %s => %s " , $( target_div ), $( target_div ).style.display , vis );
        $( target_div ).style.display = vis;
        if ( $( el_butt ) )
        {
                var butt_class = ( vis == "none" ) ? 'open' : 'close' ;
                $( el_butt ).className = "STARS-butt-" + butt_class;
                console.debug ( "--_ show_div: className :  %s" , $( el_butt ).className );
        }
};
